package com.hy.controller;

import com.hy.bean.*;
import com.hy.service.BuytableService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.*;

/**
 * (Buytable)表控制层
 *
 * @author makejava
 * @since 2024-05-14 22:03:32
 */
@CrossOrigin
@RestController
@RequestMapping("buytable")
public class BuytableController {
    /**
     * 服务对象
     */
    @Resource
    private BuytableService buytableService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @RequestMapping("/list")
    public RespBean queryByPage(String state,Integer page,Integer limit) {
        List<Map<String,Object>> buytables = new ArrayList<>();
        long count = 0l;
        try {
            buytables = buytableService.queryAllByLimit(state,page, limit);
            count = buytableService.count(state);
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("data",buytables);
        map.put("total",count);
        return RespBean.success("数据查询成功", map);
    }
    @RequestMapping("/listProducertable")
    public RespBean listProducertable(Integer page,Integer limit) {
        List<Producertable> producertable = new ArrayList<>();

        long count = 0l;
        try {
            producertable = buytableService.listProducertable(page, limit);
            count = buytableService.countProducertable();

        } catch (Exception e) {
            e.printStackTrace();
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("data",producertable);
        map.put("total",count);
        return RespBean.success("数据查询成功", map);
    }


    @RequestMapping("/details")
    public RespBean queryDetails(Integer id,String state,Integer page,Integer limit) {
        List<Map<String,Object>> buytables = new ArrayList<>();
        long count = 0l;
        try {
            buytables = buytableService.queryAllDetails("2",state,id,page, limit);
            count = buytableService.countDetails("2",state,id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("data",buytables);
        map.put("total",count);
        return RespBean.success("数据查询成功", map);
    }

    @GetMapping("queryAllByState")
    public RespBean queryAllByState(){
        return buytableService.queryAllByState();
    }

    @GetMapping("/queryAllById/{id}")
    public RespBean queryAllById(@PathVariable("id") Integer id){
        return buytableService.queryAllById(id);
    }

    @RequestMapping("/draft")
    public RespBean queryDraft(Integer id,String state,Integer page,Integer limit) {
        List<Map<String,Object>> buytables = new ArrayList<>();
        long count = 0l;
        try {
            buytables = buytableService.queryAllDetails("3",state,id,page, limit);
            count = buytableService.countDetails("3",state,id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("data",buytables);
        map.put("total",count);
        return RespBean.success("数据查询成功", map);
    }

    @RequestMapping("/statis")
    public RespBean queryStatis(String buyNumber,String buyer, String producername, String buyTime, String reachTime, Integer page, Integer limit) {
        List<Map<String,Object>> buytables = new ArrayList<>();
        long count = 0l;
        try {
            buytables = buytableService.queryAllStatis(buyNumber,buyer,producername,buyTime,reachTime,page, limit);
            count = buytableService.countStatis(buyNumber,buyer,producername,buyTime,reachTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("data",buytables);
        map.put("total",count);
        return RespBean.success("数据查询成功", map);
    }

    @RequestMapping("/deleteByidBycid")
    public RespBean deleteByidBycid(Integer id, Integer cid) throws SQLException {
        boolean b = buytableService.deleteByidBycid(id, cid);
        return RespBean.success("删除成功", b);
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/getById/{id}")
    public Buytable queryById(@PathVariable("id") Integer id) {
        return null;
    }

    /**
     * 新增数据
     *
     * @param
     * @return 新增结果
     */
    @PostMapping("/add")
    public RespBean add(@RequestBody Map<String,Object> map) throws Exception {
        boolean b = buytableService.add(map);
        if(b==true){
            return RespBean.success("添加成功", b);
        }else {
            return RespBean.success("添加失败", b);
        }
    }

    /**
     * 编辑数据
     *
     * @param buytable 实体
     * @return 编辑结果
     */
    @PutMapping("/update")
    public RespBean edit(Buytable buytable) {
        return null;
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/deleteByid/{id}")
    public RespBean deleteById(@PathVariable("id") Integer id) {
        return null;
    }

    @PostMapping("/updateInfo")
    public RespBean updateInfo(Integer id,Integer userId,String info) throws Exception {
        boolean b = buytableService.updateInfo(id,userId, info);
        return RespBean.success("审核未通过", b);
    }

    @PostMapping("/updateState")
    public RespBean updateState(Integer id,String state,Integer userId) throws Exception {
        boolean b = buytableService.updateState(id,state,userId);
        return RespBean.success("审核通过", b);
    }

    @PostMapping("/updateState2")
    public RespBean updateState2(Integer id,String state,Integer userId) throws Exception {
        boolean b = buytableService.updateUserTime(id,userId,state);
        return RespBean.success("拟定提交", b);
    }

    @RequestMapping("/vendor")
    public RespBean queryAllVendor() {
        List<Producertable> buytables = new ArrayList<>();
        try {
            buytables = buytableService.queryAllVendor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespBean.success("供应商查询成功", buytables);
    }

    @RequestMapping("/car")
    public RespBean queryCar(String name) {
        long count = 0l;
        try {
            count = buytableService.countCar(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(count>0){
            return RespBean.success("已有零件", count);
        }else {
            return RespBean.success("ok", count);
        }
    }

    @RequestMapping("/update")
    public RespBean update(int lid,String partDetailChanger,int cid,Double costPrice,Double totalPrice,int buyCount) {
        boolean b = buytableService.update(lid,cid,costPrice,buyCount,totalPrice,partDetailChanger);
        return RespBean.success("修改成功", b);
    }

    @RequestMapping("/updateCode")
    public RespBean updateCode(int cid,int id,String code,String name) throws Exception {
        int b = buytableService.updateCode(cid,code,id,name);
        return RespBean.success("提交成功", b);
    }
    /*addProducertable*/
    @PostMapping("/addProducertable")
    public RespBean addProducertable(Producertable producertable) throws Exception {
        int b = buytableService.addProducertable(producertable);
        System.out.println(producertable);
        if(b>0){
            return RespBean.success("添加成功", b);
        }else {
            return RespBean.success("添加失败", b);
        }
    }
    @PostMapping("/updateProducertable")
    public RespBean updateProducertable(@RequestBody Producertable producertable) throws Exception {
        int b = buytableService.updateProducertable(producertable);
        System.out.println(producertable);
        if(b>0){
            return RespBean.success("修改成功", b);
        }else {
            return RespBean.success("修改失败", b);
        }
    }
    @PostMapping("/deleteProducertable")
    public RespBean deleteProducertable(Integer id) throws Exception {
        int b = buytableService.deleteProducertable(id);

        if(b>0){
            return RespBean.success("删除成功", b);
        }else {
            return RespBean.success("失败", b);
        }
    }
    @RequestMapping("/list_aog")
    public RespBean queryAogByPage(String state,Integer page,Integer limit) {
        List<Map<String,Object>> buytables = new ArrayList<>();
        long count = 0l;
        try {
            buytables = buytableService.queryAllByLimit("3",page, limit);
            count = buytableService.count("3");
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("data",buytables);
        map.put("total",count);
        return RespBean.success("数据查询成功", map);
    }
    @RequestMapping("/details_aog")
    public RespBean queryDetails_aog(Integer id,Integer page,Integer limit) {
        List<Map<String,Object>> buytables = new ArrayList<>();
        long count = 0l;
        try {
            buytables = buytableService.queryAllDetails("2","3",id,page, limit);
            count = buytableService.countDetails("2","3",id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("data",buytables);
        map.put("total",count);
        return RespBean.success("数据查询成功", map);
    }
}

